** Clearing Stata memory
capture log close
clear all
set more off, perm
set seed 1234

**********************************************************************************************************************************************************
*************************************************************** Importing / Checking *********************************************************************	
**********************************************************************************************************************************************************

******************************************
********** Phase 1 dataset  **************
******************************************

/* 2008 */

insheet using "Original data/V2008_Fase1.txt"
rename inscri inscription
sum

* Changing missing observations from 0 to . if student did not show up (presf1==0)
foreach X in h1 h2 b1 b2 m1 m2 f1 f2 q1 q2 g1 g2 q13 red fase1 {
replace `X'=. if `X'==0 & presf1==0
replace `X'=0 if `X'==. & presf1==1
}

inspect

compress
sort inscription
save "Work data/work_data_P1.dta", replace
clear

/* 2004-2007 */

forvalues Z = 4/7 {
clear
insheet using "Original data/V200`Z'_Fase1.txt"
rename inscri inscription
sum
foreach X in h1 h2 b1 b2 m1 m2 f1 f2 q1 q2 g1 g2 q13 red fase1 {
replace `X'=. if `X'==0 & presf1==0
replace `X'=0 if `X'==. & presf1==1
}
append using "Work data/work_data_P1.dta"
save "Work data/work_data_P1.dta", replace
}
clear

/* 2001-2003 */

forvalues Z = 1/3 {
clear
insheet using "Original data/V200`Z'_Fase1.txt"
rename inscri inscription
sum
foreach X in h1 h2 b1 b2 m1 m2 f1 f2 q1 q2 g1 g2 q13 red fase1 {
replace `X'=. if `X'==0 & presf1==0
replace `X'=0 if `X'==. & presf1==1
}

compress
sort inscription
append using "Work data/work_data_P1.dta"
save "Work data/work_data_P1.dta", replace
}
clear

/* TEST FOR 2000 */

insheet using "Original data/V2000_Fase1.txt"
rename inscri inscription
sum

foreach X in h1 h2 b1 b2 m1 m2 f1 f2 q1 q2 g1 g2 q13 red fase1 {
replace `X'=. if `X'==0 & presf1==0
replace `X'=0 if `X'==. & presf1==1
}

compress
sort inscription
append using "Work data/work_data_P1.dta"
save "Work data/work_data_P1.dta", replace

	rename ano year
	rename q13 gen_ques_st1
	rename red essay_st1
	rename fase1 total_st1
	rename presf1 attn_st1
	rename redcorr essay_corr

	forvalues Y=1/2	{ 
	rename b`Y' biol`Y'_st1
	rename q`Y' chem`Y'_st1
	rename h`Y' hist`Y'_st1
	rename f`Y' phy`Y'_st1
	rename g`Y' geog`Y'_st1
	rename m`Y' math`Y'_st1
	}


replace essay_corr=1 if essay_corr==-1

format inscri2 %13.0g

gen enem_score=enem2 if year==2000
replace enem_score=enem if year>2000

save "Work data/work_data_P1.dta", replace